package com.example.em.service;

import com.example.em.mapper.EmployeeMapper;
import com.example.em.model.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class EmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;
    
    public List<Employee> getAllEmployees() {
        try {
            return employeeMapper.findAll();
        } catch (Exception e) {
            throw new RuntimeException("获取教师列表失败", e);
        }
    }
    
    public Employee getEmployee(Long id) {
        try {
            return employeeMapper.findById(id);
        } catch (Exception e) {
            throw new RuntimeException("获取教师信息失败", e);
        }
    }
    
    public void addEmployee(Employee employee) {
        try {
            employeeMapper.insert(employee);
        } catch (Exception e) {
            throw new RuntimeException("添加教师失败", e);
        }
    }
    
    public void updateEmployee(Employee employee) {
        try {
            employeeMapper.update(employee);
        } catch (Exception e) {
            throw new RuntimeException("更新教师信息失败", e);
        }
    }
    
    @Transactional
    public void deleteEmployee(Long id) {
        try {
            employeeMapper.deleteAttendance(id);
            employeeMapper.delete(id);
        } catch (Exception e) {
            throw new RuntimeException("删除教师失败", e);
        }
    }
} 